<?php
/**
 * tpshop
 * ============================================================================
 *
 *  网站地址:
 * ----------------------------------------------------------------------------
 * 婚礼小程序 婚礼相关流程
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用最新Thinkphp5助手函数特性实现单字母函数M D U等简写方式
 * ============================================================================
 */

namespace app\api\controller;

use think\Db;

class Married extends Base
{


    public function orderList()
    {
        $user_id = I('user_id', 0);

        $article = Db::name('married_counselor_order')->alias('a')
            ->join('__MARRIED_ORDER__ b', ' a.order_id = b.id', 'LEFT')
            ->where("a.user_id", $user_id)->select();
        if (!$article) {
            $this->ajaxReturn(['status' => -1, 'msg' => '您还没有被分配订单']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $article]);
    }

    /**
     * mike
     * 2018-7-1
     * 订单下操作步骤
     */
    public function orderItem()
    {
        if (!$this->user_id) {
            $this->ajaxReturn(['status' => -1, 'msg' => '还未登陆', 'result' => '']);
        }
        $orderCode = I('orderCode', 0);
        $step = I('step', 0);
        $article = Db::name('married_order_time_setting')->where(array('step' => $step, 'order' => $orderCode))->find();
        $warm_content = Db::name('warm_prompt')->where(array('step' => $step))->getField('content');
        if (!$article) {
            $this->ajaxReturn(['status' => 1, 'msg' => 'no_content', 'result' => $warm_content]);
        }
        $article['warm_prompt'] = $warm_content;
        $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $article]);
    }

    /**
     * mike
     * 2018-7-1
     * 设置点击时间
     */
    public function setOrderItem()
    {
        if (!$this->user_id) {
            $this->ajaxReturn(['status' => -1, 'msg' => '还未登陆', 'result' => '']);
        }

        $orderCode = I('orderCode', 0);
        $step = I('step', 0);

        $datetime = I('datetime');
        $set_time = I('set_time');

        $ret = Db::name('married_order_time_setting')->where(array('step' => $step, 'order' => $orderCode))->field('id,datetime,set_time')->find();
        if (!$ret) {
            $data['order'] = $orderCode;
            $data['step'] = $step;
            $data['datetime'] = $datetime;
            $data['set_time'] = $set_time;
            $result = Db::name('married_order_time_setting')->add($data);
        } else {
            $data['datetime'] = $datetime;
            $data['set_time'] = $set_time;
            $data['user_is_sure'] = 1;
            if ($datetime == $ret['datetime'] && $set_time == $ret['set_time']) {
                $result = true;
            } else {
                $result = Db::name('married_order_time_setting')->where('id', $ret['id'])->setField($data);
            }
        }

        if ($result) {
            $this->ajaxReturn(['status' => 1, 'msg' => '设置成功']);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '设置失败']);
        }


    }

    /**
     * mike
     * 2018-7-1
     * 设置点击时间
     */
    public function userAgreedsetOrderItem()
    {
        if (!$this->user_id) {
            $this->ajaxReturn(['status' => -1, 'msg' => '还未登陆', 'result' => '']);
        }

        $orderCode = I('orderCode', 0);
        $step = I('step', 0);


        $ret = Db::name('married_order_time_setting')->where(array('step' => $step, 'order' => $orderCode))->setField('user_is_sure', '2');

        if ($ret) {
            $this->ajaxReturn(['status' => 1, 'msg' => '设置成功']);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '设置失败']);
        }


    }

    /**
     * 获取自己的订单详情
     */
    public function getOrderDetail()
    {
        if (!$this->user_id) {
            $this->ajaxReturn(['status' => -1, 'msg' => '还未登陆', 'result' => '']);
        }

        $ret = Db::name('married_order')->where(array('user_id' => $this->user_id))->find();
        if (!$ret) {
            $this->ajaxReturn(['status' => -1, 'msg' => '还没有订单']);
        }
        $warm_content = Db::name('warm_prompt')->where(array('step' => '0'))->getField('content');
        $ret['warm_prompt'] = $warm_content;
        $ret['customer_phone1'] = married_config('customer_phone1');
        $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $ret]);
    }

    /**
     * 设置用户评价
     */
    public function setEveluate()
    {
        if (!$this->user_id) {
            $this->ajaxReturn(['status' => -1, 'msg' => '还未登陆', 'result' => '']);
        }

        $orderCode = I('orderCode', 0);
        $step = I('step', 0);
        $star = I('star'); //几星评价
        $content = I('content'); //评价内容
        $label = I('label'); //评价标签
        $starData = $_POST['starData'];
        $starData = json_decode($starData);
        $data['content'] = $content;
        $data['star'] = $star;
        $data['label'] = $label;

        foreach ($starData as $value) {
            $starList = $value->starList;
            $data[$value->keyx]=0;
            foreach ($starList as $svl) {
                if ($svl == 'hd-yduixingxingshixin font-color-active') {
                    $data[$value->keyx]++;
                }
            }
        }

        $ret = Db::name('married_order_time_setting')->where(array('step' => $step, 'order' => $orderCode))->setField($data);

        if ($ret) {
            $this->ajaxReturn(['status' => 1, 'msg' => '设置成功']);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '设置失败']);
        }


    }

    /**
     * 获取分类
     */
    public function getGoodsCat()
    {
        $cat_id = I('id');
        $ret = Db::name('goods_category')->where(array('parent_id' => $cat_id))->order('sort_order')->field('id,mobile_name as title')->select();

        if ($ret) {
            $this->ajaxReturn(['status' => 1, 'msg' => '设置成功', 'result' => $ret]);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '设置失败']);
        }
    }

    /**
     * 根据id 获取分类信息
     */
    public function getCatInfo()
    {
        $cat_id = I('id');
        $ret = Db::name('goods_category')->where(array('id' => $cat_id))->order('sort_order')->field('id,mobile_name,name')->find();

        if ($ret) {
            $this->ajaxReturn(['status' => 1, 'msg' => '设置成功', 'result' => $ret]);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '设置失败']);
        }
    }

    /**
     * 获取分类下的产品列表
     */
    public function getCatGoodsList()
    {
        $cat_id = I('id');
        $list = Db::name('goods_category')->where(array('parent_id' => $cat_id))->getField('id', true);

        if ($list != false) {
            $map['cat_id'] = array('in', $list);
        } else {
            $map['cat_id'] = $cat_id;
        }

        $goods_list = M('goods')->field('goods_id,cat_id,goods_sn,goods_name,goods_remark,market_price,shop_price,comment_count,sales_sum,original_img,click_count,is_case')->where($map)->order("sort desc")->select();
        if ($goods_list) {
            $this->ajaxReturn(['status' => 1, 'msg' => '有数据', 'result' => $goods_list]);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        }
    }

    /**
     * 联系我们
     */
    public function contactUs()
    {
        if (!$this->user_id) {
            $this->ajaxReturn(['status' => -100, 'msg' => '还未登陆', 'result' => '']);
        }
        $userInfo = $this->user;
        $phone = I('phone');
        $data['username'] = $userInfo['nickname'];
        $data['headerimg'] = $userInfo['head_pic'];
        $data['phone'] = $phone;
        $data['user_id'] = $this->user_id;
        $list = Db::name('married_user_contact_us')->where('user_id', $data['user_id'])->find();
        if ($list == false) {
            $ret = Db::name('married_user_contact_us')->add($data);
        } else {
            $ret = Db::name('married_user_contact_us')->where('user_id', $data['user_id'])->setField('phone', $phone);
        }
        if ($ret) {
            $this->ajaxReturn(['status' => 1, 'msg' => '添加成功', 'result' => $ret]);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '添加失败']);
        }
    }

    /**
     * 文章的详情
     */
    public function info()
    {
        $article_id = I('article_id/d', 0);
        $article = Db::name('married_article')->where("article_id", $article_id)->find();
        if (!$article) {
            $this->ajaxReturn(['status' => -1, 'msg' => '文章不存在']);
        }
        $article['content'] = str_replace('/public/upload/', 'https://uwedding.33designer.cn/public/upload/', $article['content']);
        $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $article]);
    }

    /**
     * 获取店铺的信息
     */
    public function getShopInfo()
    {
        $config = tpCache('shop_info');
        $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $config]);
    }

    /**
     * 菜单树形结构
     */
    public function getMenuTree()
    {
        $list = Db::name('goods_category')->where('is_show', 1)->order('parent_id_path asc,sort_order desc')->field('id,mobile_name,name,parent_id,parent_id_path,level,sort_order')->select();
        $tree = build_tree($list, 0);
        if ($tree) {
            $this->ajaxReturn(['status' => 1, 'msg' => '获取数据成功', 'result' => $tree]);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '添加失败']);
        }
    }

    /**
     * 小程序文章评价
     */
    public function articleComment()
    {
        if (!$this->user_id) {
            $this->ajaxReturn(['status' => -1, 'msg' => '还未登陆', 'result' => '']);
        }

        $content = I('content'); //评价内容
        $article_id = I('article_id');


        $data['content'] = $content;
        $data['article_id'] = $article_id;
        $data['user_id'] = $this->user_id;

        $ret = Db::name('article_comment')->add($data);

        if ($ret) {
            $this->ajaxReturn(['status' => 1, 'msg' => '设置成功']);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '设置失败']);
        }
    }

    public function articleCommentList()
    {
        $id = I('article_id');
        $list = Db::name('article_comment')->alias('a')->join('tp_users b', 'a.user_id = b.user_id')->field('b.head_pic,b.nickname,a.content,a.create_time,a.images,a.id,a.article_id')->where('a.article_id', $id)->where('is_show', 1)->order('a.create_time', 'asc')->select();
        if ($list != false) {
            $this->ajaxReturn(['status' => 1, 'msg' => '获取数据成功', 'result' => $list]);
        } else {
            $this->ajaxReturn(['status' => 2, 'msg' => '没有数据']);
        }
    }


}